# 列表推导式 格式： [表达式 for 变量 in 旧列表] 或者[表达式 for 变量 in 旧列表 if 条件]

list_name = ['tom', 'lucy', 'jack', 'bob']

# 过滤掉长度小于或者等于3的人名
# res = [name[0].upper() + name[1:] for name in list_name if len(name) > 3]
# 首字母大写
res = [name.capitalize() for name in list_name if len(name) > 3]

print(res)

# 将1-100之间能被3整除，组成一个新的列表
res = [i for i in range(1, 101) if i % 3 == 0 and i % 5 == 0]
print(res)

# 0-5偶数 0-10奇数 [(0,1),(0,3),(0,5),(2,1).....]
res = [(i, j) for i in range(0, 6) if i % 2 == 0 for j in range(0, 11) if j % 2 == 1]

print(res)

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [3, 4, 5]]
res = [i[-1] for i in list1]
print(res)

list_dict = [{"name": "tom", "salary": 5000}, {"name": "lucy", "salary": 8000}, {"name": "lily", "salary": 5500},
             {"name": "jim", "salary": 4500}]

# 薪资小于等于5000的涨500 高于5000的涨200
res = [employee['salary'] + 500 if employee['salary'] <= 5000 else employee['salary'] + 200 for employee in list_dict]
print(res)
# 更新原来的值
res = [employee['salary'] + 500 if employee['salary'] <= 5000 else employee['salary'] + 200 for employee in list_dict]
print(res)
# for employee in list_dict:
#     if employee['salary'] <= 5000:
#         employee['salary'] = employee['salary'] + 500
#     else:
#         employee['salary'] = employee['salary'] + 200
#
# print(list_dict)

print("*" * 200)
list1 = [{e: (v if e != 'salary' else (v + 200 if v > 5000 else v + 500)) for (e, v) in employee.items()} for employee
         in list_dict]

print(list1)

# 集合推导式
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [3, 4, 5]]

set1 = {y for x in list1 for y in x}
print(set1)

# 字段推导式
dict1 = {"a": "A", "b": "B", "c": "C", "d": "C"}

dict_new = {v: k for k, v in dict1.items() if k != 'b'}
print(dict_new)
